package kr.ac.kaist.savehearts.ui;

import kr.ac.kaist.savehearts.SaveHeartsConstants;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

public class ResetPasswordActivity extends Activity implements OnTouchListener {

	private static final String TAG = ResetPasswordActivity.class.getSimpleName();
	
	private EditText editTextResetEmail;
	private Button buttonOk;
	private Button buttonCancel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resetpsw);
		
		editTextResetEmail = (EditText)findViewById(R.id.editTextResetEmail);
		
		buttonOk = (Button)findViewById(R.id.buttonResetPasswordOk);
		buttonOk.setOnTouchListener(this);
		
		buttonCancel = (Button)findViewById(R.id.buttonResetPasswordCancel);
		buttonCancel.setOnTouchListener(this);
	}

	/**
	 * Check validity of e-mail address and reset password
	 * @return
	 * 	true: successfully reset password
	 * 	false: failed to reset password due to wrong e-mail address
	 */
	public boolean resetPassword() {
		String email = editTextResetEmail.getText().toString();
		
		// wrong e-mail address, show error message
		if ( !email.matches(SaveHeartsConstants.EMAIL_PATTERN) ) {
			AlertDialog errorDialog = new AlertDialog.Builder(this).create();
			errorDialog.setTitle(getString(R.string.signup));
			errorDialog.setMessage(getString(R.string.wront_signup_form));
			errorDialog.show();			
			return false;
		}
		
		// right here... send request to the server
		
		// successfully reset password!
		return true;
	}
	
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId())  {
			case R.id.buttonResetPasswordOk:
				// do something... reset password!
				if ( resetPassword() ) finish();
				editTextResetEmail.requestFocus();
				break;
			case R.id.buttonResetPasswordCancel:
				finish();
				break;
		}
		return false;
	}

}
